
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.tompai.common.lock;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

/**
* @desc: tompai-common
* @name: ReadThread.java
* @author: tompai
* @email：liinux@qq.com
* @createTime: 2021年2月7日 下午11:41:58
* @history:
* @version: v1.0
*/

public class ReadThread extends Thread{

private String sourceFile;
private String threadName;

public ReadThread(String sourceFile,String threadName){
    this.sourceFile = sourceFile;
    this.threadName = threadName;
}

@Override
public void run(){
    RandomAccessFile raf = null;
    FileChannel fc = null;
    FileLock fl = null;
    try {
        raf = new RandomAccessFile(sourceFile, "rw");
        fc = raf.getChannel();
        while(true){
            try {
                fl = fc.tryLock(0,Long.MAX_VALUE,true);
                System.out.println(fl.isShared());
                System.out.println(this.threadName+" : get the lock");
                try {
                    sleep(1000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                break;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
                System.out.println(this.threadName+" is block");
                try {
                    sleep(1000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }

        }
        StringBuffer sb = new StringBuffer();
        sb.append("Read from "+this.threadName+"：");
        ByteBuffer bb = ByteBuffer.allocate(1024);
        while((fc.read(bb))!=-1){
            //sb.append(new String(bb.array()));
            //System.out.println(new String(bb.array()));
            bb.clear();
        }
        System.out.println(this.threadName+" : read success!");
        fl.release();
            System.out.println(this.threadName+" : release lock");
        raf.close();
        fc.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

